package com.example.foodproject.controller;

import com.example.FoodProject.bean.User;
import com.example.FoodProject.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserDao dao;
    private String telphone;

    @ResponseBody
    @RequestMapping("/login")
    public Map<String,Object> login(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        try {
            if (user.getTelephone().equals("")||user.getPassword().equals("")){
                throw new Exception();
            }
        }catch (Exception e){
            map.put("success",false);
            map.put("message","账号或密码未输入");
            return map;
        }

        User user1 = dao.findOne(Example.of(user)).orElse(null);
        if (user1!=null){
            map.put("success",true);
            map.put("message","成功");
            map.put("data",user1);
        }else{
            map.put("success",false);
            map.put("message","失败");
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/login2")
    public Map<String,Object> login2(@RequestParam String telephone,@RequestParam String password){
        Map<String,Object> map = new HashMap<>();
        User user = new User(null,null,telephone,password);
        try {
            if (user.getTelephone().equals("")||user.getPassword().equals("")){
                throw new Exception();
            }
        }catch (Exception e){
            map.put("success",false);
            map.put("message","账号或密码未输入");
            return map;
        }

        User user1 = dao.findOne(Example.of(user)).orElse(null);
        if (user1!=null){
            map.put("success",true);
            map.put("message","成功");
            map.put("data",user1);
        }else{
            map.put("success",false);
            map.put("message","失败");
        }
        return map;
    }


    //使用json格式传递参数
    @RequestMapping("/registerFoJson")
    @ResponseBody
    public Map<String,Object> registerFoJson(@RequestBody User user){
        System.out.println(user);
        User user1 = dao.saveAndFlush(user);
        Map<String,Object> map = new HashMap<>();
        if (user1!=null){
            map.put("success",true);
            map.put("message","成功");
        }else{
            map.put("success",false);
            map.put("message","失败");
        }
        return map;
    }

    @RequestMapping("/registerFoParam")
    @ResponseBody
    //不使用json格式传递参数，地址栏输入参数  ?name=小名&telephone=5555&password=5555
    public Map<String,Object> registerFoParam(@RequestParam String name, @RequestParam String password, @RequestParam String telephone){
        User user = new User(null,name,telephone,password);
        User user1 = dao.saveAndFlush(user);
        Map<String,Object> map = new HashMap<>();
        if (user1!=null){
            map.put("success",true);
            map.put("message","成功");
        }else{
            map.put("success",false);
            map.put("message","失败");
        }
        return map;
    }


}
